import { createRouter, createWebHashHistory } from "vue-router";
import routes from "./routes";
import { loading } from "@/utils/loading";
import { useUserStore } from "@/stores";

import NProgress from "nprogress" // 进度条条


const router = createRouter({
  routes,
  history: createWebHashHistory(),
  scrollBehavior() {
    return {
      top: 0,
    };
  },
});
const whiteList = ["/login"];

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (!window.existLoading) {
    loading.show();
    window.existLoading = true;
  }
  const userStore = useUserStore();

  if (whiteList.includes(to.path) || userStore.token) {
    next();
  } else {
    next("/login");
  }
});

router.afterEach((to, from) => {
  if (window.existLoading) {
    loading.hide();
  }
  NProgress.done()
});

export default router;
